CREATE OR REPLACE PACKAGE BODY Mtaskr
IS
------------------------------------------------------------------------------------------------------------------------------------------------
-- TASK HTP Write
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE task_write (
      pid            NUMBER,
      pdescription   VARCHAR2,
      pcreator       VARCHAR2,
      pcreated       DATE
   )
   IS
   BEGIN
      Htx.PRINT (   TO_CHAR (pid)
                 || dlm
                 || pdescription
                 || dlm
                 || pcreator
                 || dlm
                 || TO_CHAR (pcreated)
                );
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
-- BIT HTP Write
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_write (
      ptask_id       NUMBER,
      pid            NUMBER,
      pdescription   VARCHAR2,
      pcreator       VARCHAR2,
      pcreated       DATE
   )
   IS
   BEGIN
      Htx.PRINT (   TO_CHAR (ptask_id)
                 || dlm
                 || TO_CHAR (pid)
                 || dlm
                 || pdescription
                 || dlm
                 || pcreator
                 || dlm
                 || TO_CHAR (pcreated,'YYYY-MM-DD')||'T'||TO_CHAR (pcreated,'HH24:MI:SS')||'Z' --2008-01-28T20:24:17Z
                );
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
-- Load all Tasks for a User
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE task_init (puser VARCHAR2, pspace VARCHAR2)
   IS
      bfirst   BOOLEAN := TRUE;
   BEGIN
      FOR x IN (SELECT   *
                    FROM MTASKR_TASKS
                   WHERE creator = puser AND status > -1 AND space_id = TO_NUMBER(pspace)
                ORDER BY sort_date ASC)
      LOOP
         IF bfirst
         THEN
            bfirst := FALSE;
         ELSE
            Htx.PRINT (dlm2);
         END IF;

         task_write (x.ID, x.description, x.creator, x.created);
      END LOOP;
   END;

 ------------------------------------------------------------------------------------------------------------------------------------------------
-- TASK Create New
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE task_create (pdescription VARCHAR2, pcreator VARCHAR2, pspace VARCHAR2)
   IS
      dcreated   DATE   := SYSDATE;
      nid        NUMBER;
   BEGIN
      INSERT INTO MTASKR_TASKS
                  (description, creator, created, assigned,space_id
                  )
           VALUES (pdescription, pcreator, dcreated, pcreator,TO_NUMBER(pspace)
                  );

      COMMIT;

      SELECT ID
        INTO nid
        FROM MTASKR_TASKS
       WHERE created = dcreated AND creator = pcreator;

      task_write (nid, pdescription, pcreator, dcreated);
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
--  TASK Delete
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE task_del (pid VARCHAR)
   IS
   BEGIN
      UPDATE MTASKR_TASKS
         SET status = -1
       WHERE ID = pid;

      COMMIT;
      Htx.PRINT ('0');
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
-- TASK set up
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE task_move (pid VARCHAR, pdir VARCHAR2 DEFAULT '+x')
   IS
      dtempdate   DATE := SYSDATE;
   BEGIN
      IF pdir = '+x'
      THEN
         UPDATE MTASKR_TASKS
            SET sort_date = SYSDATE
          WHERE ID = TO_NUMBER (pid);
      END IF;

      IF pdir = '-x'
      THEN
         SELECT MIN (sort_date) - 1
           INTO dtempdate
           FROM MTASKR_TASKS;

         UPDATE MTASKR_TASKS
            SET sort_date = dtempdate
          WHERE ID = TO_NUMBER (pid);
      END IF;

      COMMIT;
      Htx.PRINT (pdir);
   END;

   ------------------------------------------------------------------------------------------------------------------------------------------------
-- BIT Load all Bits for a User
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_init (puser VARCHAR2, ptask_id VARCHAR2)
   IS
      bfirst   BOOLEAN := TRUE;
   BEGIN
      FOR x IN (SELECT   *
                    FROM MTASKR_BITS
                   WHERE task_id = TO_NUMBER (ptask_id)
                     AND creator = puser
                     AND status > -1
                ORDER BY sort_date ASC)
      LOOP
         IF bfirst
         THEN
            bfirst := FALSE;
         ELSE
            Htx.PRINT (dlm2);
         END IF;

         bit_write (TO_NUMBER (ptask_id), x.ID, x.text, x.creator, x.created);
      END LOOP;
   END;

   ------------------------------------------------------------------------------------------------------------------------------------------------
-- BIT Load all Bits for a User
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_init_deleted (puser VARCHAR2, ptask_id VARCHAR2)
   IS
      bfirst   BOOLEAN := TRUE;
   BEGIN
      FOR x IN (SELECT   *
                    FROM MTASKR_BITS
                   WHERE task_id = TO_NUMBER (ptask_id)
                     AND creator = puser
                     AND status = -1
                ORDER BY sort_date ASC)
      LOOP
         IF bfirst
         THEN
            bfirst := FALSE;
         ELSE
            Htx.PRINT (dlm2);
         END IF;

         bit_write (TO_NUMBER (ptask_id), x.ID, x.text, x.creator, x.created);
      END LOOP;
   END;
   
   
 ------------------------------------------------------------------------------------------------------------------------------------------------
-- BIT Create New
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_create (ptask_id VARCHAR2, ptext VARCHAR2, pcreator VARCHAR2)
   IS
      dcreated   DATE   := SYSDATE;
      nid        NUMBER;
   BEGIN
      INSERT INTO MTASKR_BITS
                  (text, creator, created, assigned, task_id
                  )
           VALUES (ptext, pcreator, dcreated, pcreator, TO_NUMBER (ptask_id)
                  );

      COMMIT;

      SELECT ID
        INTO nid
        FROM MTASKR_BITS
       WHERE created = dcreated AND creator = pcreator;

      bit_write (TO_NUMBER (ptask_id), nid, ptext, pcreator, dcreated);
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
--  BIT  Delete
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_del (pid VARCHAR)
   IS
   BEGIN
      UPDATE MTASKR_BITS
         SET status = -1, sort_date = SYSDATE
		 
       WHERE ID = pid;

      COMMIT;
      Htx.PRINT ('0');
   END;

------------------------------------------------------------------------------------------------------------------------------------------------
-- TASK set up
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE bit_move (pid VARCHAR, pdir VARCHAR2 DEFAULT '+x')
   IS
      dtempdate   DATE := SYSDATE;
   BEGIN
      IF pdir = '+x'
      THEN
         UPDATE MTASKR_BITS
            SET sort_date = SYSDATE
          WHERE ID = TO_NUMBER (pid);

		  -- If task is awakened from the tomb (status = -1), update status 
         UPDATE MTASKR_BITS
            SET status = 1
          WHERE ID = TO_NUMBER (pid) AND status = -1;		  
      END IF;

      IF pdir = '-x'
      THEN
         SELECT MIN (sort_date) - 1
           INTO dtempdate
           FROM MTASKR_BITS;

         UPDATE MTASKR_BITS
            SET sort_date = dtempdate
          WHERE ID = TO_NUMBER (pid);
      END IF;

      COMMIT;
      Htx.PRINT (pdir);
   END;
------------------------------------------------------------------------------------------------------------------------------------------------
-- User Init
------------------------------------------------------------------------------------------------------------------------------------------------
   PROCEDURE user_init (q VARCHAR2, LIMIT VARCHAR2, TIMESTAMP VARCHAR2)
   IS
   BEGIN
        IF q = '@' THEN
   		FOR  x IN (SELECT * FROM MTASKR_USER)
		LOOP
		        Htp.PRINT ('@'||x.nick||'|'||x.email);
		END LOOP;
		END IF;


   END;
------------------------------------------------------------------------------------------------------------------------------------------------
END Mtaskr;
/

